package com.huatai.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.CollectionUtil;
import com.huatai.datacenter.config.CustomDataModelException;
import com.huatai.datacenter.entity.datamodel.*;
import com.huatai.datacenter.mapper.MappingModelMapper;
import com.huatai.datacenter.service.MappingModelFieldService;
import com.huatai.datacenter.service.MappingModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 映射模型服务实现类
 *
 * @author lizhong
 * @date 2023年8月2日
 */
@Service
public class MappingModelServiceImpl extends ServiceImpl<MappingModelMapper, MappingModelEntity> implements MappingModelService {

	@Autowired
	private MappingModelFieldService mappingModelFieldService;

	/**
	 * 获取映射模型分页列表
	 *
	 * @param mappingModelEntity(pageIndex,pageSize,mappingModelName,mappingModelTypeId)
	 * @return
	 * @author lizhong
	 * @date 2023年8月2日
	 */
	public Page<MappingModelEntity> selectMappingModelList(MappingModelEntity mappingModelEntity) {
		// 查询类
		LambdaQueryWrapper<MappingModelEntity> mappingModelQuery = Wrappers.lambdaQuery();

		// select * from tb_mapping_model where mapping_model_name is not null; 按照更新时间排序
		mappingModelQuery
			.eq(DataModelBaseEntity::getDeleted, false)
			.isNotNull(MappingModelEntity::getMappingModelName)
			.orderByDesc(DataModelBaseEntity::getUpdateTime);

		// 分页参数
		Page<MappingModelEntity> page = new Page<>(mappingModelEntity.getPageIndex(), mappingModelEntity.getPageSize());
		Page<MappingModelEntity> mappingModelEntityPage = page(page, mappingModelQuery);

		// 也要把映射模型字段填上去，后续点击修改按钮时就能调用了
		for (MappingModelEntity recordData : mappingModelEntityPage.getRecords()) {
			List<MappingModelFieldEntity> mappingModelFieldEntities = mappingModelFieldService.getMappingModelFieldList(recordData.getId());
			recordData.setFields(mappingModelFieldEntities);
		}
		return mappingModelEntityPage;
	}

	/**
	 * 插入一个映射模型
	 *
	 * @param mappingModelEntity
	 * @return
	 * @date 2023年8月2日
	 */
	@Transactional(rollbackFor = {Exception.class})
	public boolean insertMappingModel(MappingModelEntity mappingModelEntity) {
		// TODO: 判断已存在?

		// 插入mapping_model表
		boolean save = save(mappingModelEntity);

		// 插入多条mapping field
		List<MappingModelFieldEntity> fields = mappingModelEntity.getFields();
		if (CollectionUtil.isNotEmpty(fields)) {
			for (MappingModelFieldEntity field : fields) {
				field.setMappingModelId(mappingModelEntity.getId());
			}
			// 自带批量插入方法
			mappingModelFieldService.saveBatch(fields);
		}
		// 返回是否成功
		return save;
	}

	/**
	 * 更新映射模型名字和字段，删除旧的再存新的
	 *
	 * @param mappingModelEntity
	 * @return
	 */
	@Transactional(rollbackFor = {Exception.class})
	public boolean updateMappingModel(MappingModelEntity mappingModelEntity) {
		// 字段，要与数据模型的字段一一对应
		List<MappingModelFieldEntity> fields = mappingModelEntity.getFields();
		List<Long> fieldIds = new ArrayList<>(fields.size());
		for (MappingModelFieldEntity field : fields) {
			field.setMappingModelId(mappingModelEntity.getId());
			fieldIds.add(field.getId());
		}
		LambdaQueryWrapper<MappingModelFieldEntity> fieldEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
		fieldEntityLambdaQueryWrapper
			.eq(MappingModelFieldEntity::getDeleted, false)
			.eq(MappingModelFieldEntity::getMappingModelId, mappingModelEntity.getId())
			.notIn(MappingModelFieldEntity::getId, fieldIds);
		MappingModelFieldEntity mappingModelFieldEntity = new MappingModelFieldEntity();
		// 删除旧的
		mappingModelFieldEntity.setDeleted(true);
		mappingModelFieldService.update(mappingModelFieldEntity, fieldEntityLambdaQueryWrapper);
		// 存新的
		mappingModelFieldService.saveOrUpdateBatch(mappingModelEntity.getFields());
		return updateById(mappingModelEntity);
	}

	/**
	 * 根据id删除映射模型
	 *
	 * @param mappingModelEntitys
	 * @return
	 * @date 2023年8月3日
	 */
	@Transactional(rollbackFor = {Exception.class})
	public boolean deleteByIds(List<MappingModelEntity> mappingModelEntitys) {
		List<MappingModelEntity> deleteDataModelList = new ArrayList<>(mappingModelEntitys.size());

		List<Long> deleteFields = new ArrayList<>(mappingModelEntitys.size());

		// 删除映射模型
		for (MappingModelEntity mappingModelEntity : mappingModelEntitys) {

			mappingModelEntity.setDeleted(true);
			deleteDataModelList.add(mappingModelEntity);
			deleteFields.add(mappingModelEntity.getId());
		}

		MappingModelFieldEntity deleteField = new MappingModelFieldEntity();
		deleteField.setDeleted(true);
		updateBatchById(deleteDataModelList);
		LambdaQueryWrapper<MappingModelFieldEntity> deleteFieldQuery = Wrappers.lambdaQuery();

		// 删除对应字段
		deleteFieldQuery
			.in(MappingModelFieldEntity::getMappingModelId, deleteFields);
		return mappingModelFieldService.update(deleteField, deleteFieldQuery);
	}
}
